import type { optionsItem } from '@/types/pricker';
import { ref } from 'vue';

export const useGetAreaJson = () => {
  const areaPrickerOptions = ref<optionsItem[]>([]);
  const regAreaPrickerOptions = ref<optionsItem[]>([]);

  const getAreaJson = async () => {
    const areaOptions: optionsItem[] = [];
    const regAreaOptions: optionsItem[] = [];
    interface ListItem {
      name: string;
      code: string;
      children?: ListItem[];
    }
    const res = await import('@/assets/area.json');
    const list: ListItem[] = res.default;
    if (list.length) {
      list.forEach((v1, idx1: number) => {
        areaOptions.push({
          text: v1.name,
          value: v1.code,
        });
        regAreaOptions.push({
          text: v1.name,
          value: v1.code,
        });
        if (v1.children?.length) {
          areaOptions[idx1].children = [];
          regAreaOptions[idx1].children = [];
          v1.children.forEach((v2, idx2) => {
            areaOptions[idx1]?.children?.push({
              text: v2.name,
              value: v2.code,
            });
            regAreaOptions[idx1]?.children?.push({
              text: v2.name,
              value: v2.code,
            });
            if (v2.children?.length) {
              (regAreaOptions[idx1].children as optionsItem[])[idx2].children = [];
              v2.children.forEach((v3) => {
                regAreaOptions[idx1].children?.[idx2]?.children?.push({
                  text: v3.name,
                  value: v3.code,
                });
              });
            }
          });
        }
      });
    }

    areaPrickerOptions.value = areaOptions;
    regAreaPrickerOptions.value = regAreaOptions;
  };
  getAreaJson();

  return [areaPrickerOptions, regAreaPrickerOptions];
};
